package com.lxj.api;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lxj.model.Pic;
import com.lxj.model.UserInfo;
import com.lxj.network.http.HttpUtils;
import com.lxj.utils.L;
import com.lxj.utils.PublicFunction;
import com.lxj.utils.ZlibUtils;


public class ApiUtils {

	public static String getResponseCode(String aCode) {
		if (aCode != null && aCode.equals("0")) {
			aCode = "network_error";
		} else if (aCode != null && aCode.equals("1")) {
			aCode = "cancel";
		}
		return aCode;
	}

	public static String getResponseData(Header[] headers, byte[] aResponseData) {
		byte[] responsedata = aResponseData;

		if (HttpUtils.iSResponseNeedDecompress(headers)) {
			responsedata = ZlibUtils.decompress(aResponseData);
		}

		String ret = PublicFunction.bytes2String(responsedata);
		responsedata = null;

		return ret;
	}

	public static JSONObject getResponseJSON(Header[] headers,
			byte[] aResponseData) {
		String response = getResponseData(headers, aResponseData);
		L.e("Response", response != null && response.length() > 0 ? response: "none");
		JSONObject ret = null;
		try {
			ret = new JSONObject(response);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static String parseSocketMsgid(JSONObject aJson) {
		return PublicFunction.getJSONString(aJson, "msgid");
	}

	public static String parseSocketType(JSONObject aJson) {
		return PublicFunction.getJSONString(aJson, "type");
	}

	public static JSONArray parseSocketData(JSONObject aJson) {
		return PublicFunction.getJSONArray(aJson, "data");
	}

	public static String parseResultCode(JSONObject aResponseData) {
		JSONObject resultCode = PublicFunction.getJSONObject(aResponseData,ApiConfig.RESULT_INFO);
		return PublicFunction.getJSONString(resultCode, ApiConfig.RESULT_CODE);
	}

	public static JSONObject parseResultData(JSONObject aResponseData) {
		return PublicFunction.getJSONObject(aResponseData,
				ApiConfig.RESULT_DATA);
	}

	public static Pic parsePic(JSONObject aJson) {
		String pic_id = PublicFunction.getJSONString(aJson, "pic_id");
		String pic_url = PublicFunction.getJSONString(aJson, "pic_url");

		Pic tPic = new Pic();
		tPic.setPic_id(PublicFunction.String2int(pic_id));
		tPic.setPic_url(pic_url);
		return tPic;
	}
	public static UserInfo parseUserInfo(JSONObject aJson) {
		int user_id = PublicFunction.getJSONInt(aJson, "user_id");
		String name = PublicFunction.getJSONString(aJson, "name");
		JSONObject head = PublicFunction.getJSONObject(aJson, "head");
		String sex = PublicFunction.getJSONString(aJson, "sex");
		String birthday = PublicFunction.getJSONString(aJson, "birthday");
		String introduction =PublicFunction.getJSONString(aJson, "introduction");
		UserInfo t = new UserInfo();
		t.setUser_id(user_id);
		t.setUser_name(name);
		t.setUser_head(parsePic(head));
		t.setUser_sex(PublicFunction.String2int(sex));
		t.setUser_birthday(PublicFunction.String2long(birthday));
		t.setUser_introduction(introduction);
		return t;
	}
	
	
	
	



}
